class Visitor < ActiveRecord::Base
  belongs_to :user,
             :class_name => "user",
             :foreign_key => :visited_id
  
  def self.exist?(visitor,visited)
    not find_by_visitor_id_and_visited_id(visitor,visited).nil?
  end
  
  def self.find_visitors(visitor,visited)
    return visitor.visitors if visitor == visited
    if self.exist?(visitor,visited)
      v = find_by_visitor_id_and_visited_id(visitor,visited)
      v.updated_at = Time.now
      v.save
    else
      v = create(:visitor_id=>visitor.id)
      visited.visitors<< v
    end
    v = visited.visitors
    if v.length>10
      l = v.last
      v.destroy(l)
    end
    visited.visitors
  end
  
end
